﻿// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;
using System.Windows.Media;

using Nova.CodeDOM;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.TypeParameter"/>.
    /// </summary>
    public class TypeParameterVM : CodeObjectVM
    {
        #region /* STATICS */

        internal static void AddViewModelMapping()
        {
            CreateViewModel.Add(typeof(TypeParameter),
                delegate(CodeObject codeObject, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary) { return new TypeParameterVM((TypeParameter)codeObject, dictionary); });
        }

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.TypeParameter"/>.
        /// </summary>
        public TypeParameterVM(TypeParameter typeParameter, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(typeParameter, dictionary)
        { }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.TypeParameter"/> model.
        /// </summary>
        public TypeParameter TypeParameter
        {
            get { return (TypeParameter)CodeObject; }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        public override Brush BorderBrush
        {
            get { return DarkPalerGreen; }
        }

        public override Brush BackgroundBrush
        {
            get { return PalerGreen; }
        }

        public override void Render(CodeRenderer renderer, RenderFlags flags)
        {
            RenderFlags passFlags = (flags & RenderFlags.PassMask);
            if (flags.HasFlag(RenderFlags.PrefixSpace))
                renderer.RenderText(" ", PUNC_BRUSH, ParentVM);

            if (flags.HasFlag(RenderFlags.Description))
            {
                renderer.RenderText("(type parameter) ", NORMAL_BRUSH, TextStyle.Proportional);
                CodeObjectVM parent = ParentVM;
                if (parent is TypeDeclVM)
                    ((TypeDeclVM)parent).RenderName(renderer, RenderFlags.Description);
                else if (parent is GenericMethodDeclVM)
                    ((GenericMethodDeclVM)parent).RenderName(renderer, RenderFlags.Description);
                renderer.RenderText(Dot.ParseToken, PUNC_BRUSH, this);
            }
            else
                RenderAnnotations(renderer, passFlags);
            renderer.RenderText(TypeParameter.Name, IDENTIFIER_BRUSH, this);
        }

        public static void RenderTypeParameters(CodeRenderer renderer, ChildListVM<TypeParameterVM> typeParameterVMs, RenderFlags flags)
        {
            RenderFlags passFlags = (flags & RenderFlags.PassMask);
            // Render the angle brackets normally in the GUI, even if we're inside a documentation comment
            CodeObjectVM parent = typeParameterVMs.Parent;
            renderer.RenderText(TypeParameter.ParseTokenStart, PUNC_BRUSH, parent);
            renderer.RenderList(typeParameterVMs, passFlags, parent);
            renderer.RenderText(TypeParameter.ParseTokenEnd, PUNC_BRUSH, parent);
        }

        public override void RenderToolTip(CodeRenderer renderer, RenderFlags flags)
        {
            TypeRefBaseVM.RenderType(renderer, CodeObject.GetType(), flags, this);
            renderer.RenderText(" :  ", NORMAL_BRUSH, TextStyle.Proportional | TextStyle.Bold);
            // Just render the TypeParameter name instead of as a Description
            renderer.RenderText(TypeParameter.Name, IDENTIFIER_BRUSH, this);
            RenderMessagesInToolTip(renderer, flags);
        }

        #endregion
    }
}
